<template>
  <div class="article-container">
    <!-- 上半截 -->
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!--  面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="small">
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <!-- el-radio 默认把label作为文本和选中之后的value值 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="channelId" placeholder="请选择">
            <el-option label="全部" :value="null">
            </el-option>
            <el-option :label="channel.name" :value="channel.id"
            v-for="(channel,index) in channels" :key="index">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadArticle(1)" :disabled="loading">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>根据筛选条件共询{{totalCount}}条结果：</span>
      </div>
      <!-- 数据列表 -->

      <!-- table 表格组件
            1. 把需要展示的数组列表数据绑定给table组件的data属性
            注意：不需要自己v-for遍历，它自己会自动遍历
            2、设计表格列el-table-column
               width可以设定表格列的宽度
               label 可以设定列的标题
               prop用来设定要渲染的列表项数据字段
            3. 表格列默认只能渲染普通文本，如果需要展示其他内容，如按钮、图片，
            就需要自定义表格列模板
        -->
      <el-table
        :data="articles"
        size="small"
        style="width: 100%"
        stripe
        border
        class="list-table"
        v-loading="loading"
      >
        <el-table-column
          prop="cover"
          label="封面"
          width="auto"
        >
        <template slot-scope="scope">
          <!-- <img v-if="scope.row.cover.images[0]" alt="" class="article-cover" :src="scope.row.cover.images[0]">
          <img v-else src="./wu.jpg" alt="" class="article-cover"> -->
           <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.cover.images[0]"
              fit="cover" lazy>
            </el-image>
        </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="280">
        </el-table-column>
        <!-- 如果需要在自动义列模板中获取当前遍历项数据，那么就在template中声明
        slot-scope="scope" -->
        <el-table-column label="状态">
          <template  slot-scope="scope">
            <el-tag :type="articleStatus[scope.row.status].type">
              {{articleStatus[scope.row.status].text}}
            </el-tag>
            <!-- <el-tag type="warning" v-else-if="scope.row.status===1">待审核</el-tag>
            <el-tag type="success" v-else-if="scope.row.status===2">审核通过</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status===3">审核失败</el-tag>
            <el-tag type="info" v-else-if="scope.row.status===4">已删除</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布日期"
          width="180"
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <!-- 如果需要自定义表格列模板，则把需要自定义的内容放到 template里面 -->
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="$router.push('/publish?id='+scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination background layout="prev, pager, next"
    :total="totalCount"
    :disabled="loading"
    :page-size="pageSize"
    :current-page.sync="page"
    @current-change="onCurrentChange"
    />
  </div>
</template>
<script>
import { getArticleInfo, getArticleChannels, deleteArticle } from '@/api/article'
export default {
  name: 'ArticleIndex',
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      articles: [], // 文章数据列表
      articleStatus: [
        { status: 0, text: '草稿', type: 'info' },
        { status: 1, text: '待审核', type: '' },
        { status: 2, text: '审核通过', type: 'success' },
        { status: 3, text: '审核失败', type: 'warning' },
        { status: 4, text: '已删除', type: 'danger' }
      ],
      totalCount: 0, // 总数据条数
      pageSize: 15, // 每页的大小
      status: null, // 查询文章的状态，不穿就是全部
      channels: [], // 文章频道列表
      channelId: null, // 查询文章的频道
      rangDate: null, // 筛选的范围日期
      loading: true,
      page: 1 // 分页页码
    }
  },
  created() {
    this.loadArticle(1)
    this.loadChannels()
  },
  methods: {
    // 获取文章列表
    loadArticle(page = 1) {
      this.loading = true // 展示加载中 loading
      getArticleInfo({
        page,
        per_page: this.pageSize,
        status: this.status, // 根据状态查询
        channel_id: this.channelId, // 根据频道ID查询
        begin_pubdate: this.rangDate ? this.rangDate[0] : null, // 开始时间
        end_pubdate: this.rangDate ? this.rangDate[1] : null// 截止时间
      }).then((res) => {
        const { results, total_count: totalCount } = res.data.data
        this.articles = results
        this.totalCount = totalCount
        // console.log(this.channelId)

        // 关闭加载
        this.loading = false
      })
    },
    // 获取分页更新
    onCurrentChange(page) {
      this.loadArticle(page)
      // console.log(page)
    },
    // 获取文章频道
    loadChannels() {
      getArticleChannels().then(res => {
        // console.log(res)
        this.channels = res.data.data.channels
      })
    },
    // 删除文章
    delArticle(articleId) {
      // 找到数据接口
      // 封装请求方法
      // 删除请求调用
      // 处理响应结果
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行这里
        deleteArticle(articleId.toString()).then(res => {
          // 删除成功，更新当前页的文章数据列表
          this.loadArticle(this.page)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  components: {},
  props: []
}
</script>

<style scoped lang = "less">
.article-container {
  .box-card {
    margin-top: 5px;
  }
  .list-table {
    margin-bottom: 20px;
  }
  .article-cover{
    width:80px;
    height: 80px;
  }
}
</style>
